package com.woniuxy.java106crm.service.impl;

import com.woniuxy.java106crm.entity.Perms;
import com.woniuxy.java106crm.mapper.PermsMapper;
import com.woniuxy.java106crm.service.PermsService;
import com.woniuxy.java106crm.vo.PermsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户权限业务逻辑层
 */
@Slf4j
@Service
public class PermsServiceImpl implements PermsService {
    @Resource
    private PermsMapper permsMapper;

    //角色分配权限 查询全部权限
    @Override
    public List<Perms> queryPerms() {
        List<Perms> permsList = permsMapper.queryPerms();
//        log.debug("{}",permsList);
        List<Perms> father = new ArrayList<>();
        for (Perms perms : permsList){
            if(perms.getParentid().equals(0)){
                father.add(perms);
            }
        }
        for (Perms perms : father){
            List<Perms> children = new ArrayList<>();
            for(Perms perms1 : permsList){
                if(perms1.getParentid().equals(perms.getId())){
                    children.add(perms1);
                }
            }
            perms.setChildren(children);
        }
//        log.debug("{}",father);
        return father;
    }

    @Override
    public Perms selectById(int id) {
        return permsMapper.selectById(id);
    }

    @Override
    public boolean updatePerms(Perms perms) {
        return permsMapper.updatePerms(perms);
    }

    @Override
    public boolean addPerms(PermsVo permsVo) {
        return permsMapper.addPerms(permsVo);
    }
}
